﻿using System.Diagnostics;
using System.Threading.Tasks;
using System;
using System.Threading;

namespace TaskResult
{
    internal class TaskCalculate
    {
        /// <summary>
        /// 基础task运用
        /// </summary>
        public  static  void Start() 
        {
            try
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();  //启动Stopwatch
                Console.WriteLine("Stopwatch is running:{0}", stopwatch.IsRunning);//获取当前Stopwatch的状态
                var task1 = Task.Run(() => Calculate(2));
                var task2 = Task.Run(() => Calculate(1));

                //var a = await task1;
                //var b = await task2;
                Console.WriteLine($"线程执行结束，task1线程结果为{task1.Result * 1000},task2线程结果为{task2.Result * 1000}");
                Console.WriteLine("The second RunTime:{0}", stopwatch.ElapsedMilliseconds);//这里面使用毫秒来输出
                Console.WriteLine("===================================================");


                Stopwatch stopwatch2 = new Stopwatch();
                stopwatch2.Start();  //启动Stopwatch
                Console.WriteLine("Stopwatch is running:{0}", stopwatch2.IsRunning);//获取当前Stopwatch的状态
                var task3 = Task.Run(() => Calculate(2));
                var task4 = Calculate(1);

                //var c = await task3;

                Console.WriteLine($"线程执行结束，task1线程结果为{task3.Result * 1000},task2线程结果为{task4 * 1000}");
                Console.WriteLine("The second RunTime:{0}", stopwatch2.ElapsedMilliseconds);//这里面使用毫秒来输出
                Console.WriteLine("===================================================");
            }
            catch (Exception)
            {

                throw;
            }
            

        }

        /// <summary>
        /// 耗时计算业务
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static int Calculate(int param)
        {
            Thread.Sleep(param * 1000);
            Console.WriteLine($"线程休息{param}秒结束");
            return param * 1000;
        }

    }
}
